using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace ASA
{
    public partial class LoginDialog : ASA.BaseDialog
    {
        private AandEDLL.DataAccess.User user;

        public LoginDialog()
        {
            InitializeComponent();
            base.Title = "User Login";
            user = null;
        }


        private void btnLogin_Click(object sender, EventArgs e)
        {
            try
            {
                user = AandEDLL.Accessor.AuthenticateUser(tbUserName.Text, tbPassword.Text);
            }
            catch (Exception ex)
            {
                if(ex.Message == "Invalid user name and password.")
                    MessageBox.Show(ex.Message, "ASA ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                else
                MessageBox.Show(this, "Unable to access the database. Please make sure the \n" +
                    "database is present or that SQLExpress is installed.", "ASA Error", MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
               
            }
            if (user == null)
            {
                DialogResult = DialogResult.None;
            }
            else
            {
                user.LastLoginDate = DateTime.Now;
                AandEDLL.Accessor.UpdateUser( user );
                DialogResult = DialogResult.OK;                
            }
        }

        public AandEDLL.DataAccess.User GetUser
        {
            get
            {
                return user;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
        }

        private void btnHelp_Click(object sender, EventArgs e)
        {
            Help.ShowHelp(this, "appraisal.chm");
            DialogResult = DialogResult.None;
        }
    }
}

